//
//  SaveCachesTool.m
//  jiulongLJ
//
//  Created by MyMac on 15/4/9.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "SaveCachesTool.h"
#import "NSString+Hashing.h"
#import "FMDB.h"

@interface SaveCachesTool()

@property(nonatomic,strong)FMDatabaseQueue*queue;
@end
@implementation SaveCachesTool
+(SaveCachesTool*)shareSaveTool//单利
{
    static SaveCachesTool*tool;
    
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        tool = [[self alloc] init] ;
    }) ;
    
    return  tool;
    
}
+(BOOL)saveCachesPath:(NSString *)pathName andcachesData:(id)requestData
{
      NSString*str=   [self receicePath];
       NSString*path=[str stringByAppendingPathComponent:[pathName MD5Hash]];

    BOOL isSucceed=[requestData writeToFile:path atomically:YES];
    return isSucceed;
}
+(BOOL)fileExiste:(NSString*)pathName
{
     NSString*str=   [self receicePath];
    NSString*path=[str stringByAppendingPathComponent:[pathName MD5Hash]];
    
    return [[NSFileManager defaultManager]fileExistsAtPath:path];



}
+(id)receiveCachesData:(NSString *)pathName
{
    NSString*str=   [self receicePath];

    NSString*path=[str stringByAppendingPathComponent:[pathName MD5Hash]];
        NSDictionary*dict=[NSDictionary dictionaryWithContentsOfFile:path];

        return dict;

}
+(BOOL)removefile:(NSString *)pathName
{
    NSFileManager*manager=[NSFileManager defaultManager];
     NSString*str=   [self receicePath];

    NSString*path=[str stringByAppendingPathComponent:[pathName MD5Hash]];
    NSError*error=nil;
  BOOL ret=[manager removeItemAtPath:path error:&error];
    NSLog(@"%d",ret);
     if(ret==NO)
         NSLog(@"删除文件失败%@",error);
    return ret;

}
+(NSString*)receicePath
{
    NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSError*err=nil;
    BOOL der=  [[NSFileManager defaultManager]createDirectoryAtPath:[str stringByAppendingPathComponent:@"Internet/file"] withIntermediateDirectories:YES attributes:nil error:&err];
    NSLog(@"%ld",(long)der);
    return [str stringByAppendingPathComponent:@"Internet/file"];
    
}
+(BOOL)removeAllFile
{
 NSFileManager*manager=[NSFileManager defaultManager];
 NSString*str=   [self receicePath];

    NSError*error=nil;
    BOOL ret=[manager removeItemAtPath:str error:&error];
  
    if(ret==NO)
    {
        NSLog(@"所有文件没有删除成功%@",error);
    }
    return ret;

}
//-(void)createLibraryPath:(NSString*)path//创建数据库
//{
//    NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//    NSString*filename=[str stringByAppendingPathComponent:path];
//    Log(@"表＊＊＊＊%@",filename);
//    // 1.创建数据库队列
//   if([[NSFileManager defaultManager]fileExistsAtPath:filename]==NO){
//    self.queue = [FMDatabaseQueue databaseQueueWithPath:filename];
//    }
//
//
//}

//@"create table if not exists Content_Model (mark integer primary key autoincrement, Id text,Title text,AbstractContent text,ContentType text, StoreNum text,TitleImage text,IsRecommend text,IsTop text,PraiseNum text,TeacherPraiseNum text,ViewNum text,PlayTime text,SetNo text,ReleaseTime text,LabelList text,Label text,LabelId text,LabelName text);"
+(BOOL)createTabelSqliteLanguage:(NSString*)language andpath:(NSString*)path//创表
{
    SaveCachesTool*tool=[SaveCachesTool shareSaveTool];
    NSString*str=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString*filename=[str stringByAppendingPathComponent:path];
    NSLog(@"表＊＊＊＊%@",filename);
    // 1.创建数据库队列
    if([[NSFileManager defaultManager]fileExistsAtPath:filename]==NO||tool.queue==nil){
        tool.queue = [FMDatabaseQueue databaseQueueWithPath:filename];
    }

   // 2.创表
     __block BOOL result;
    [tool.queue inDatabase:^(FMDatabase *db) {
        
        result= [db executeUpdate:language];
        
        if (result) {
            NSLog(@"创表成功");
        } else {
            NSLog(@"创表失败");
        }
    }];

   return result;

}

//@"insert into t_student (name, age) values (?, ?);"
//[db executeUpdate:@"insert into namedparamcounttest values (:a, :b, :c, :d)" withParameterDictionary:dictionaryArgs]
//[db executeUpdate:@"DROP TABLE User"]
//@"ALTER TABLE Teachers ADD COLUMN name text";
+(BOOL)insertdataSqliteLanguage:(NSString*)language and:(NSDictionary*)dict//增
{
     SaveCachesTool*tool=[SaveCachesTool shareSaveTool];
   //NSString*sqlite=[NSString stringWithFormat:@"%@  %@",tool,tool.queue];
    NSLog(@"%@  %@",tool,tool.queue);
    __block BOOL result;
    [tool.queue inDatabase:^(FMDatabase *db) {
//        if(dict==nil){
//        result=[db executeUpdate:language];
//        }else{
        result =[db executeUpdate:language withParameterDictionary:dict];//根据字典存储数据
    //    }
    }];
    return  result;
    
}
//@"delete from t_student where ID=?"
+(BOOL)deleteDataSqliteLanguage:(NSString*)language//删
{
    SaveCachesTool*tool=[SaveCachesTool shareSaveTool];
    __block BOOL result;
    [tool.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
      result=  [db executeUpdate:language];
        
    }];
    return result;
}
//@"update t_student set age = ? where name = ?;"
+(BOOL)updataDataSqliteLanguage:(NSString*)language//改
{
    SaveCachesTool*tool=[SaveCachesTool shareSaveTool];
    __block BOOL result;
    [tool.queue inDatabase:^(FMDatabase *db) {
     result=[db executeUpdate:language];

    }];

    return  result;
}
//@"select * from t_student where age > ?;"
+(NSArray*)searchDataSqliteLanguage:(NSString*)language
{
    SaveCachesTool*tool=[SaveCachesTool shareSaveTool];
    NSMutableArray*array=[[NSMutableArray alloc]init];
    [tool.queue inDatabase:^(FMDatabase *db) {
        FMResultSet *rs = [db executeQuery:language];
        while (rs.next) {
        NSDictionary*dict=[rs resultDictionary];
            [array addObject:dict];
        }
    }];
    return  array;
}
-(void)dealloc
{

    NSLog(@"缓存%@",self.class);

}
@end
